﻿@using A2.ERP.Models
@{
    List<t_GroupAuthority> groupAuthority = ViewBag.groupAuthority;
    List<t_Authority> authority = ViewBag.authority;
}

<section id="EditGroup">
    <form action="/setting/editGroup" method="post" class="frm">
        <input type="text" name="groupId" value="@ViewBag.groupId" class="d-none" />
        <table class="table">
            <thead>
                <tr>
                    <th>选择
                        <input type="checkbox" name="selectAll" class="checkbox" />
                    </th>
                    <th>权限名称</th>
                    <th>权限描述</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in authority)
            {
                    <tr>
                        <td>
                            <input type="checkbox" value="@item.AuthorityID" name="id" class="checkbox" />
                        </td>
                        <td>@item.AuthorityName</td>
                        <td>@item.Description</td>
                    </tr>
                }
            </tbody>
        </table>
        <button class="btn btn-success">修改</button>
    </form>
    <pre>
        
    </pre>
</section>

<script>
    ;
    !function () {
        var $allSelect = $('input[name=selectAll]');

        var toggleSelect = function () {
            var count = 1;

            return function () {
                $('#EditGroup .checkbox').each(function (i, k) {
                    k.checked = count % 2 != 0 ? true : false;
                });
                count++;
            }
        };

        $allSelect.click(toggleSelect());

        var data = @Html.Raw(Json.Encode(groupAuthority.Select(i => i.AuthorityID)));
        $('#EditGroup .checkbox').each(function (i, k) {
            k.checked = (data.indexOf(parseInt(k.getAttribute('value'))) > 0);
        });

        $('#EditGroup .frm').submit(function (e) {
            e.preventDefault();

            $('#EditGroup .frm').ajaxSubmit({
                type: 'PUT',
                success: function () {
                    $tab.close($('#EditGroup'));
                }
            })
        });
    }();
</script>